var salt = sessionStorage.getItem('usersalt');
var maxEstimateNum = 0;
var optionType = 0;
var id = getQueryVariable("id");
var questionId = getQueryVariable("questionId");
var paperid = getQueryVariable("paperid");
var questionList = [];
var answerList = [];
var sortType;
var indexNow = 0;
console.log(questionId)

var app = new Vue({
  el: '.body',
  data: {
    info: '',
    list: "",
    indexNow: '',
    answerItem:'',
  },
  updated: function () {
  console.log(this.list);
  }
})


$.post(url + "questionPaper/getInfo", {id: paperid, Token: salt}, function (data) {
  if (data.code == 0) {
    var data = data.data;
    initQuestion(data);
    $.post(url + "questionUser/getInfo", {id: id, Token: salt}, function (data) {
      if (data.code == 0) {
        var data = data.data;
        app.info = data;
        sortType.forEach(function (item) {
          var list = JSON.parse(data[item.replace("Question", "")]);
          list.forEach(function (it) {
            answerList.push(it);
          })
        })
        app.answerItem = answerList[indexNow];
        console.log(app.answerItem);
      } else {
        alert(data.msg)
      }
    });

  } else {
    alert(data.msg)
  }
});


function initQuestion(data) {
  $(".question_name").text(data.title);
  //题目排序 0:单选，1：多选，2：判断
  sortType = data.sortType.split("—");
  sortType.splice(sortType.indexOf("单选"), 1, "radioQuestion");
  sortType.splice(sortType.indexOf("多选"), 1, "multiSelectQuestion");
  sortType.splice(sortType.indexOf("判断"), 1, "tfngQuestion");
  sortType.forEach(function (item) {
    data[item].forEach(function (list, index) {
      list.options = JSON.parse(list.options);
      questionList.push(list);
    })
  })
  for (var i = 0; i < questionList.length; i++) {
    if (questionId == questionList[i].id) {
      app.indexNow = indexNow = i;
      app.list = questionList[i];
    }
  }
}

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
  var form = layui.form,
      layer = parent.layer === undefined ? layui.layer : top.layer,
      $ = layui.jquery,
      laydate = layui.laydate,
      laytpl = layui.laytpl,
      table = layui.table;

  if (!sessionStorage.getItem('username')) {
    layer.msg('请先登录');
    setTimeout("top.location.href='../../../index.html'", 1000);
  }
});


//首题
$("body").on("click", ".first", function () {
  app.indexNow = indexNow = 0;
  app.list = questionList[0];
  app.answerItem = answerList[indexNow];
});
$("body").on("click", ".last", function () {
  app.indexNow = indexNow = questionList.length - 1;
  app.list = questionList[indexNow];
  app.answerItem = answerList[indexNow];
});
$("body").on("click", ".prev", function () {
  if (indexNow > 0) {
    indexNow--;
    app.indexNow--;
    app.list = questionList[indexNow];
    app.answerItem = answerList[indexNow];
  } else {
    alert("当前是第一个题目！");
  }
});
$("body").on("click", ".next", function () {
  if (indexNow < questionList.length - 1) {
    indexNow++;
    app.indexNow++;
    app.list = questionList[indexNow];
    app.answerItem = answerList[indexNow];
  } else {
    alert("当前是最后一个题目！");
  }
});


















